/*
 * Created by Jaren at 2021/9/26 15:49
 */

import 'package:flutter_blue_plus/flutter_blue_plus.dart';

abstract class BleDeviceFilter {
  /// should the target device be accepted.
  /// TRUE - should NOT be filtered
  /// FALSE - should be filtered
  bool accept(ScanResult scanResult);

  static final defaultFilter = BleDeviceEmptyFilter();
}

class BleDeviceEmptyFilter extends BleDeviceFilter {
  @override
  bool accept(ScanResult scanResult) {
    return true;
  }
}

/// A filter by ble deice name.
class BleDeviceNameFilter extends BleDeviceFilter {
  RegExp? regex;

  BleDeviceNameFilter(String regex) {
    this.regex = RegExp(regex);
  }

  @override
  bool accept(ScanResult scanResult) {
    if (regex == null) return true;
    return (regex!.hasMatch(scanResult.device.advName) != false ||
            scanResult.device.advName.contains('-')) ||
        regex!.hasMatch(scanResult.device.advName);
  }
}
